package com.iot.inter.controller.newinterface.sync;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.response.subResponse.SynchronousPartnerResponse;
import com.iot.inter.entity.newpojo.response.subResponse.common.Page;
import com.iot.inter.entity.newpojo.response.subResponse.common._Partner;
import com.iot.inter.entity.newrequest.sync.syncpartner.request.RequestSyncPartner;
import com.iot.inter.entity.newrequest.sync.syncpartner.response.ResponseSyncPartner;
import com.iot.inter.entity.newrequest.sync.syncpartner.response.ResponseSyncPartnerList;
import com.iot.inter.interfaceprocessing.newinterface.TrunToSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.17 同步企业信息：2.0接口
 * <p>
 * 该接口主要提供给领科内部系统，比如套餐商品管理系统，
 * 来获取目前IOT平台中接入的所有合作企业信息，以便能够更加方便的管理企业的套餐商品。
 *
 * @author lm
 * @date 2019/8/29
 */
@RestController
@Api(description = "3.17 同步企业信息")
public class SyncPartnerController {
    private static Logger logger = Logger.getLogger(SyncPartnerController.class);

    @CrossOrigin
    @RequestMapping(value = "syncPartner", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.17 同步企业信息", response = ResponseSyncPartner.class)
    public String syncPartner(@ApiParam @RequestBody RequestSyncPartner request) {
        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        if (requestStr != null) {

            logger.info("【同步企业信息】请求：" + requestStr);

            RequestSyncPartner requestPackage = JSONArray.parseObject(requestStr, RequestSyncPartner.class);
            Integer pageNo = requestPackage.getPageNo();
            Integer pageSize = requestPackage.getPageSize();

            TrunToSyncService sync = new TrunToSyncService();
            SynchronousPartnerResponse response = sync.syncPartner(pageNo, pageSize);

            if (response != null) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(response.getHeader().getResultCode())) {

                    List<ResponseSyncPartnerList> tradeDataList = new ArrayList<ResponseSyncPartnerList>();

                    Page page = null;

                    if (response.getBody() != null) {

                        ResponseSyncPartnerList tradeData = null;

                        if (response.getBody().getPartner() != null) {

                            for (_Partner p : response.getBody().getPartner()) {

                                tradeData = new ResponseSyncPartnerList();

                                tradeData.setPartnerCode(p.getPartnerCode());
                                tradeData.setPartnerName(p.getPartnerName());
                                tradeData.setPartnerType(p.getPartnerType());
                                tradeData.setSecret(p.getSecret());
                                tradeData.setConnector(p.getConnector());
                                tradeData.setCurrency(p.getCurrency());
                                tradeData.setEmail(p.getEmail());
                                tradeData.setPhoneNum(p.getPhoneNum());
                                tradeData.setRelatedSaleName(p.getSaleName());

                                tradeDataList.add(tradeData);

                            }

                            page = new Page();
                            page.setCurrentPageNo(response.getBody().getPage().getCurrentPageNo());
                            page.setPageSize(response.getBody().getPage().getPageSize());
                            page.setTotalCount(response.getBody().getPage().getTotalCount());
                            page.setTotalPages(response.getBody().getPage().getTotalPages());
                        }

                    }

                    ResponseSyncPartner responseOrder = new ResponseSyncPartner();
                    responseOrder.setRetCode(response.getRetCode());
                    responseOrder.setResultInfo(response.getResultInfo());
                    responseOrder.setTradeData(tradeDataList);
                    responseOrder.setPartnerNum(response.getBody().getPartnerNum());
                    responseOrder.setPage(page);
                    jsonString = JSON.toJSONString(responseOrder);

                    logger.info("【同步企业信息】成功响应：" + jsonString);
                    return jsonString;

                } else {

                    ResponseSyncPartner responseOrder = new ResponseSyncPartner();
                    responseOrder.setRetCode(response.getRetCode());
                    responseOrder.setResultInfo(response.getResultInfo());
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【同步企业信息】响应：" + jsonString);
                    return jsonString;
                }
            } else {

                ResponseSyncPartner responseOrder = new ResponseSyncPartner();
                responseOrder.setRetCode("9998");
                responseOrder.setResultInfo("响应报文为空");
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【同步企业信息】响应：" + jsonString);
                return jsonString;

            }

        } else {

            ResponseSyncPartner responseOrder = new ResponseSyncPartner();
            responseOrder.setRetCode("9998");
            responseOrder.setResultInfo("请求报文为空");
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【同步企业信息】响应：" + jsonString);
            return jsonString;


        }
    }

}
